package jp.dev7.android.database;

import java.util.Arrays;

import android.database.Cursor;
import android.test.AndroidTestCase;

public class ByteArrayCarryCursorTest extends AndroidTestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testGetBlob() {

        byte[] data1 = new byte[] { 1, 1, 1 };
        byte[] data2 = new byte[] { 2, 2, 2 };
        byte[] data3 = new byte[] { 3, 3, 3 };

        ProtoBufCursor bcursor = new ProtoBufCursor();
        bcursor.addRow(new Object[] { data1 });
        bcursor.addRow(new Object[] { data2 });
        bcursor.addRow(new Object[] { data3 });

        Cursor cursor = bcursor;

        assertEquals(3, cursor.getCount());
        assertEquals(-1, cursor.getPosition());

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data1, cursor.getBlob(0)));

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data2, cursor.getBlob(0)));

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data3, cursor.getBlob(0)));

        assertFalse(cursor.moveToNext());

        assertTrue(cursor.moveToFirst());
        assertFalse(cursor.moveToPrevious());

        assertTrue(cursor.moveToLast());
        assertFalse(cursor.moveToNext());
    }

    public void testGetBlob1() {

        byte[] data1 = new byte[] { 1, 1, 1 };
        ProtoBufCursor bcursor = new ProtoBufCursor().add(data1);
        Cursor cursor = bcursor;

        assertEquals(1, cursor.getCount());
        assertEquals(-1, cursor.getPosition());

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data1, cursor.getBlob(0)));

        assertFalse(cursor.moveToNext());

        assertTrue(cursor.moveToFirst());
        assertFalse(cursor.moveToPrevious());

        assertTrue(cursor.moveToLast());
        assertFalse(cursor.moveToNext());
    }

    public void testGetBlob2() {

        byte[] data1 = new byte[] { 1, 1, 1 };
        byte[] data2 = new byte[] { 2, 2, 2 };
        byte[] data3 = new byte[] { 3, 3, 3 };

        ProtoBufCursor bcursor = new ProtoBufCursor();
        bcursor.addRow(Arrays.asList(new Object[] { data1 }));
        bcursor.addRow(Arrays.asList(new Object[] { data2 }));
        bcursor.addRow(Arrays.asList(new Object[] { data3 }));

        Cursor cursor = bcursor;

        assertEquals(3, cursor.getCount());
        assertEquals(-1, cursor.getPosition());

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data1, cursor.getBlob(0)));

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data2, cursor.getBlob(0)));

        assertTrue(cursor.moveToNext());
        assertTrue(Arrays.equals(data3, cursor.getBlob(0)));

        assertFalse(cursor.moveToNext());

        assertTrue(cursor.moveToFirst());
        assertFalse(cursor.moveToPrevious());

        assertTrue(cursor.moveToLast());
        assertFalse(cursor.moveToNext());
    }

}
